package com.mjfeed.service.impl;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import com.mjfeed.Principal;
import com.mjfeed.dto.api.MemberJSON;
import com.mjfeed.entity.Member;
import com.mjfeed.service.MemberService;
import com.mjfeed.util.Md5Util;
import com.mjfeed.util.Pagination;
import com.mjfeed.util.ParamBean;
import com.mjfeed.util.QueryCondition;

@Service("memberService")
public class MemberServiceImpl extends BaseServiceImpl implements MemberService {

	@Transactional(readOnly = true)
	public Pagination<Member> getPagination(ParamBean paramBean) {
		List<QueryCondition> queryConditions = new ArrayList<QueryCondition>();
		if (StringUtils.isNotBlank(paramBean.getKeyword())) {
			queryConditions.add(new QueryCondition(paramBean.getField(), QueryCondition.LK, paramBean.getKeyword()));
		}
		String orderBy = "order by id desc";
		Pagination<Member> pagination = (Pagination<Member>)getPagination(Member.class, queryConditions, orderBy, paramBean.getPage(), paramBean.getPageSize());
		pagination.setPageHtml(paramBean.getUrl());
		return pagination;
	}
	
	@Transactional(readOnly = true)
	public boolean isAuthenticated() {
		RequestAttributes requestAttributes = RequestContextHolder.currentRequestAttributes();
		if (requestAttributes != null) {
			HttpServletRequest request = ((ServletRequestAttributes) requestAttributes).getRequest();
			Principal principal = (Principal) request.getSession().getAttribute(Member.PRINCIPAL_ATTRIBUTE_NAME);
			if (principal != null) {
				return true;
			}
		}
		return false;
	}

	@Transactional(readOnly = true)
	public Member getCurrent() {
		RequestAttributes requestAttributes = RequestContextHolder.currentRequestAttributes();
		if (requestAttributes != null) {
			HttpServletRequest request = ((ServletRequestAttributes) requestAttributes).getRequest();
			Principal principal = (Principal) request.getSession().getAttribute(Member.PRINCIPAL_ATTRIBUTE_NAME);
			if (principal != null) {
				return getById(Member.class, principal.getId());
			}
		}
		return null;
	}

	@Transactional(readOnly = true)
	public String getCurrentUsername() {
		RequestAttributes requestAttributes = RequestContextHolder.currentRequestAttributes();
		if (requestAttributes != null) {
			HttpServletRequest request = ((ServletRequestAttributes) requestAttributes).getRequest();
			Principal principal = (Principal) request.getSession().getAttribute(Member.PRINCIPAL_ATTRIBUTE_NAME);
			if (principal != null) {
				return principal.getUsername();
			}
		}
		return null;
	}

	@Transactional(readOnly = true)
	public boolean usernameExists(String username) {
		List<QueryCondition> queryConditions = new ArrayList<QueryCondition>();
		queryConditions.add(new QueryCondition("username",QueryCondition.EQ,username));
		Long count = getRecordCount(Member.class, queryConditions);
		return (count != null && count > 0) ? true : false;
	}

	@Transactional(readOnly = true)
	public boolean usernameDisabled(String username) {
		return false;
	}

	@Transactional(readOnly = true)
	public boolean emailExists(String email) {
		List<QueryCondition> queryConditions = new ArrayList<QueryCondition>();
		queryConditions.add(new QueryCondition("email",QueryCondition.EQ,email));
		Long count = getRecordCount(Member.class, queryConditions);
		return (count != null && count > 0) ? true : false;
	}

	@Transactional(readOnly = true)
	public boolean emailUnique(String previousEmail, String currentEmail) {
		if (StringUtils.equalsIgnoreCase(currentEmail, previousEmail)) {
			return true;
		} else {
			if (!emailExists(currentEmail)) {
				return true;
			} else {
				return false;
			}
		}
	}
	
	@Transactional(readOnly = true)
	public Member getMemberByUsername(String username) {
		List<QueryCondition> queryConditions = new ArrayList<QueryCondition>();
		queryConditions.add(new QueryCondition("username",QueryCondition.EQ,username));
		Member member = (Member) getSingleResult(Member.class, queryConditions);
		return member;
	}

	@Override
	public MemberJSON memberToJSON(Member member) {
		if (member != null) {
			MemberJSON memberJson = new MemberJSON();
			memberJson.setUsername(member.getUsername());
			memberJson.setEmail(member.getEmail());
			memberJson.setMobile(member.getMobile());
			memberJson.setName(member.getName());
			StringBuilder sign = new StringBuilder();
			sign.append("username=").append(member.getUsername()).append("&password=").append(member.getPassword());
			memberJson.setSign(Md5Util.encrypt(sign.toString()));
			return memberJson;
		}
		return null;
	}

	@Override
	public long getCount() {
		return getRecordCount(Member.class, null);
	}

	@Override
	public Member getLastMember() {
		String orderBy = "order by id desc";
		List<Member> members = get(Member.class, null, orderBy);
		if (members != null && members.size() > 0) {
			return members.get(0);
		}
		return null;
	}
}
